<?php
/**
 * 文章模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: ArticleModel.class.php 7548 2011-05-31 09:54:33Z richie $
 * 
 */
class ArticleModel extends CommonModel {
	
	/**
	 * 底部帮助文档
	 *
	 * @return array
	 */
	public function getHelps(){
		$helpcate = D('ArticleCat')->getHelpCate();
		if(empty($helpcate)) return array();
		foreach($helpcate as $key=>$cate){
			$helpcate[$key]['helps'] = $this->getArticleByCate($cate['id'], 'help');
		}
		return $helpcate;
	}
	
	/**
	 * 公告
	 *
	 * @return array
	 */
	public function getNotice($limit=5, $page=1){
		$notice = $this->getArticleByCate(11, 'notice', $limit, $page);
		return $notice;
	}
	
	/**
	 * 指定分类的文章列表
	 *
	 * @param int $id 分类ID
	 * @param $action 操作名
	 * @param $limit 显示条数
	 * @param $page  当前页
	 * @return array
	 */
	public function getArticleByCate($id=0, $action='article', $limit=10, $page=1){
		$article = $this->where(array('cat_id'=>$id, 'is_open'=>1))
					->field('article_id as id, title, link')
					->limit($limit)
					->page($page)
					->order('article_id DESC')
					->findAll();
		if(empty($article)) return array();
		foreach($article as $key=>$art){
			$article[$key]['url'] = U('mall://'.$action, array('id'=>$art['id']));
		}
		return $article;
	}
	
	/**
	 * 下一篇文章
	 *
	 * @param int $article_id
	 * @param int $cat_id
	 * @param string $action
	 * @return array
	 */
	public function getNextArticle($article_id, $cat_id, $action='article'){
		$map['_string'] = 'article_id<'.$article_id;
		if($cat_id) $map['cat_id'] = $cat_id;
		$row = $this->where($map)->field('article_id as id,title,link')->find();
		if($row){
			$row['url'] = (!empty($row['link']) && $row['link'] != 'http://') ? $row['link'] : U('mall://'.$action, array('id'=>$row['id']));
		}
		return $row;
	}
	
	/**
	 * 上一篇文章
	 *
	 * @param int $article_id
	 * @param int $cat_id
	 * @param string $action
	 * @return array
	 */
	public function getPrevArticle($article_id, $cat_id, $action='article'){
		$map['_string'] = 'article_id>'.$article_id;
		if($cat_id) $map['cat_id'] = $cat_id;
		$row = $this->where($map)->field('article_id as id,title,link')->find();
		if($row){
			$row['url'] = (!empty($row['link']) && $row['link'] != 'http://') ? $row['link'] : U('mall://'.$action, array('id'=>$row['id']));
		}
		return $row;
	}
}
?>